// index.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({
  data: {
    navBarActive:0,
    navBarList:[
      {
        label: '热门推荐',
        value: 0
      },
      {
        label: '金牌师傅',
        value: 1
      },
      {
        label: '优秀门店',
        value: 2
      },
      {
        label: '排行榜',
        value: 3
      },
      {
        label: '公司介绍',
        value: 4
      }
    ],
    current: 0,
    thumb: [],
    cateOne: [],
    cate: [],
    cateGoods:[],
    goodsList:[],
    height: '',
    menuH: '32px',
    safeTop: `40px`,
    address: '',
    nearSkill: [],
    config: '',
    isGoToFlag:false,
    couponShow: false,
    closeOnClickOverlay: false,
    couponList:[], // 优惠券数据
    page: 1,
    adImgs: [],
    seckillData: {}, // 秒杀商品
    countDownNum: 0,

    finish: false,
    loading: false,
    page: 1,
  },


  getHomeSeckillList(){
    http.get('/addons/shopro/goods/homeSeckillList?type=ing').then(res => {
      const num = new Date().getTime();
      this.setData({
        seckillData: res.data,
        countDownNum: res.data.endtime - num,
      })
    })
  },
  onSlideChangeEnd: function(e) {
    this.setData({
      current: e.detail.current
    });
  },
  adImgsTap(e){
    let item = e.currentTarget.dataset.item;
    util.skip('/'+item.url)
  },
  getADImgs(){
    http.post('index/extend').then(res => {
      this.setData({
        adImgs: res.data
      })
    })
  },
  goSeckill(){
    util.skip('/service/seckill/seckill')
  },
  goNewGift(){
    util.skip('/service/newGift/newGift')
  },
  getCouponList(){
    http.get('coupon/getlist', {
      page: this.data.page,
    }).then(res => {
      this.setData({
        couponList: res.data || []
      })
    })
  },
  couponTap(e){
    // 立即领取
    if(e.detail.type == 1){
      http.get('coupon/receive', {
        type: 0,
        coupon_id: e.detail.info.id
      }).then(res => {
        util.toast(res.msg)
        this.getCouponList()
      })
    }else if(e.detail.type == 2){
      wx.switchTab({
        url: '/pages/index/index'
      })
    }
  },
  onClose(){
    this.setData({
      couponShow:false
    })
  },
  serviceIndexTap(e){
    let item = e.currentTarget.dataset.item;
    util.skip('/service/servedetail/servedetail?id='+item.id+'&seckill_id='+item.seckill_id)
  },
  goToTop(){
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
  },
  bindNavBar(e){
    let item = e.currentTarget.dataset.item;
    this.setData({
      navBarActive: item.value
    })
    if(item.label == '金牌师傅'){
      // wx.switchTab({ url: '/pages/servers/servers' })
      util.skip('/pages/servers/servers')
    }else if(item.label == '优秀门店'){
      // wx.switchTab({ url: '/pages/business/business' })
      util.skip('/pages/business/business')
    }else if(item.label == '排行榜'){
      util.skip('/service/ranking/ranking')
    }else if(item.label == '公司介绍'){
      util.skip('/service/company/company')
    }
  },
  selectAddress(){
    util.skip('/service/selectAddress/selectAddress?city='+this.data.address.city+'&name='+this.data.address.name)
  },
  search(){
    util.skip('/service/searchdetail/searchdetail?type=1')
  },
  nearSkill(){
    wx.switchTab({
      url: '/pages/servers/servers'
    })
  },
  cateList(e){
    let name = e.currentTarget.dataset.item.label ? e.currentTarget.dataset.item.label : e.currentTarget.dataset.item.name
    util.skip('/service/catelist/catelist?category_id='+e.currentTarget.dataset.item.id+'&name='+name)
  },
  swiperTap(e) {
      let item = e.currentTarget.dataset.item;
      if(item.type == 1){
        util.skip('/service/servedetail/servedetail?id='+item.goods_id)
      }else if(item.type == 2){
        util.skip('/service/info/info?id='+item.config_text_id)
      }else if(item.type == 3){
        if(item.jump.indexOf('pages/') != -1){
          wx.switchTab({
            url: item.jump,
          })
        }else {
          util.skip(item.jump)
        }
      }
  },
  center() {
    this.onClose();
    wx.switchTab({
      url: '/pages/activity/activity',
    })
    // util.authSkip('/service/couponscenter/couponscenter')
  },
  //跳转服务者详情
  skillTap(e){
    util.skip('/service/skilldetail/skilldetail?skill_id='+e.currentTarget.dataset.info.id)
  },
  serviceTap(e){
    util.skip('/service/servedetail/servedetail?id='+e.detail.id)
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  totypedetail(e) {
    wx.navigateTo({
      url: '/service/tentype/tentype?name=' + e.currentTarget.dataset.name,
    })
  },
  go() {
    wx.navigateTo({
      url: '/service/addressmanager/addressmanager',
    })
  },
  getInfo(){
    http.post('thumb/getlist', '', true).then(res => {
      this.setData({
        thumb: res.data
      })
    })

    http.post('category/getlist', '', true).then(res => {
      const data = res.data;
      this.setData({
        cate: data,
        cateOne : data.splice(0, 5)
      })
    })
  },
  getCityInfo(){
    http.get('skill/nearskill', {
      city: this.data.address.city,
      lat: this.data.address.lat,
      lng: this.data.address.lng,
      page: 1
    }, false).then(res => {
      this.setData({
        nearSkill: res.data
      })
    })
  },
  getIsShowCoupon(){
    http.post('coupon/ispopshow', {city: this.data.address.city}, false).then(res => {
      this.setData({
        couponShow: res.data.is_show === 1
      })
    })
  },
  // 获取热门服务数据
  getCategorygoods(){
    http.post('goods/categorygoods', {city: this.data.address.city}, false).then(res => {
      this.setData({
        cateGoods: res.data,
        goodsList: res.data[0].goodsList
      })
    })
  },

  reload() {
    this.setData({
      list: [],
      page: 1,
      finish: false
    })
    this.getList()
  },
  more() {
    this.setData({
      page: ++this.data.page
    })
    this.getList()
  },
  getList() {
    if (this.data.finish) {
      return
    }
    if (this.data.loading) {
      return
    }
    this.setData({
      loading: true
    })
    let data = {
      page: this.data.page,
      is_home_hot: 1,
    }
    http.get('goods/getList', data).then(res => {
      if (res.data.length === 0) {
        this.setData({
          finish: true
        })
      }
      let arr = this.data.goodsList.concat(res.data)
      this.setData({
        goodsList: arr
      })

    }).finally(res => {
      this.setData({
        loading: false
      })
    })
  },
  onLoad(option) {
    console.error('option--',option)
    wx.setStorageSync('parent_user_id', option.parent_user_id ? option.parent_user_id : '')

    this.setData({
      safeTop: `${app.globalData.safeTop}px`,
      menuH: `${app.globalData.menuH}px`,
    })
    this.getInfo();
    this.getIsShowCoupon();
    this.getCouponList();
    // this.getCategorygoods();
    this.getADImgs();
    this.getHomeSeckillList();
    this.getList();
  },
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
  },
  onLoadConfig(option){
    this.setData({
      config: app.globalData.config
    })
  },
  onShowAddress(option){
    this.setData({
      address: app.globalData.address
    })
    // this.getCityInfo()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.more();
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {}
  },
  onPageScroll(e){
    this.setData({
      isGoToFlag: e.scrollTop >= 600
    })
  }
})
